define(function(require, exports, module){
	
	require("../nuui-environment");
	
	var headerTemp = require("text!./nuui-header.html");
	
	/**
	 * @class Header
	 * @memberof Nuui
	 * @classdesc 继承自Backbone.Marionette.ItemView,应用头部的实现<br/>
	 * 		App.header即为此类
	 */
	var Header = module.exports = Marionette.ItemView.extend({
		
		// 如果是native,头部没有东西
		template:Nuui.os == "web" ? headerTemp : "<div></div>",

		tagName:"header",
		
		onRender: function(){
			var view = this;

			view.$title = view.$(".header_title");
			view.$leftButton = view.$(".header_btn_left");
			view.$rightButton = view.$(".header_btn_right");
			
			// 左右按钮tap时触发对应的自定义buttonclick事件
			view.$leftButton.on("tap", function(){
				view.triggerButtonLeftClick();
				return false;
			});
			view.$rightButton.on("tap", function(){
				view.triggerButtonRightClick();
				return false;
			});
			
			// 自定义buttonclick触发时执行exec方法
			this.on("button:left:click", this.execLeftButtonHandler);
			this.on("button:right:click", this.execRightButtonHandler);
			
			// 如果是网页版,绑定active
			if(Nuui.os == "web"){
				var beginEvent = ("ontouchstart" in document) ? "touchstart" : "mousedown";
				var endEvent = ("ontouchend" in document) ? "touchend" : "mouseup";
				
				this.$(".header_btn_left").bind(beginEvent, function(){
					if(this.hasChildNodes()){
						$(this).addClass("active");
					}
				}).bind(endEvent, function(){
					if(this.hasChildNodes()) {
						$(this).removeClass("active");
					}
				});
				
				this.$(".header_btn_right").bind(beginEvent, function(){
					if(this.hasChildNodes()) {
						$(this).addClass("active");
					}
				}).bind(endEvent, function(){
					if(this.hasChildNodes()) {
						$(this).removeClass("active");
					}
				})
			}




		/*	/!*分享*!/
            //全局变量，动态的文章ID
            var ShareURL = "";
            //绑定所有分享按钮所在A标签的鼠标移入事件，从而获取动态ID
            $(function () {
                $(".bdsharebuttonbox a").mouseover(function () {
                    ShareURL = $(this).attr("data-url");
                });
            });

            /!*
            * 动态设置百度分享URL的函数,具体参数
            * cmd为分享目标id,此id指的是插件中分析按钮的ID
            *，我们自己的文章ID要通过全局变量获取
            * config为当前设置，返回值为更新后的设置。
            *!/
            function SetShareUrl(cmd, config) {
                if (ShareURL) {
                    config.bdUrl = ShareURL;
                }
                return config;
            }

            //插件的配置部分，注意要记得设置onBeforeClick事件，主要用于获取动态的文章ID
            window._bd_share_config = {
                "common": {
                    onBeforeClick: SetShareUrl, "bdSnsKey": {}, "bdText": ""
                    , "bdMini": "2", "bdMiniList": false, "bdPic": "http://www.baidu.com", "bdStyle": "0", "bdSize": "24"
                }, "share": {}
            };
            //插件的JS加载部分
            with (document) 0[(getElementsByTagName('head')[0] || body)
                .appendChild(createElement('script'))
                .src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='
                + ~(-new Date() / 36e5)];*/
		},

		onShow:function () {
			$(".close").on("tap",function () {
				$("#modal").css({
					"display":"none"
				})
            })
        },

		contents:{
			"@back":"<span class='iconfont icon-leftarrow'> 返回</span>",
            "@back1":"<a href='javascript :history.back(-1)' style='color: #FFF;'><span class='iconfont icon-leftarrow'>我的</span></a>",
            "@back2":"<a href='javascript :history.back(-1)' style='color: #FFF;'><span class='iconfont icon-leftarrow'>云讲座</span></a>",
            "@back3":"<a href='javascript :history.back(-1)' style='color: #FFF;'><span class='iconfont icon-leftarrow'>云采购</span></a>",
            "@back4":"<a href='javascript :history.back(-1)' style='color: #FFF;'><span class='iconfont icon-leftarrow'>首页</span></a>",
            "@back5":"<a href='javascript :history.back(-1)' style='color: #FFF;'><span class='iconfont icon-leftarrow'>服务大厅</span></a>",
            "@back6":"<a href='javascript :history.back(-1)' style='color: #FFF;'><span class='iconfont icon-leftarrow'>云项目</span></a>",
            "@cate":"<a href='javascript :history.back(-1)' style='color: #FFF;'><span class='cate_btn iconfont icon-fenlei' style='font-size:.36rem;'></span></a>",
			"@exit":"退出",
			"wode":"我的",
			"@share":"<img src='images/img/fenxiang.png'/>",
			"@index":"<i class='header_btn_home'></i>",
			"@select":"<img src='images/img/dangdajing.png' style='width: 49%;float: right'/>",
            "@set":"<img src='images/img/gerenzhongxin(luo).png'/>"
		},

		leftButtonHandler:null,
		rightButtonHandler:null,

		defaultHandler:function(content){
			/*if(content == "@exit"){
				return function(){
					confirm("你真的要退出吗?");
				}

			}
			*/
			if(content == "wode"||content=="@cate"){

				return false;
			}
			if(content == "@menu"){
				return function(){
					App.nav.show();	
				}
			}
            if(content == "@set"){
                return function(){
                    App.navigate("example/index/set", true);
                    return false;
                }
            }
            if(content == "@share"){
                return function(){
                    $("#modal").css({
						"display":"block"
					})
                    return false;
                }
            }
            if(content == "@select"){
            	var urlPath=location.href;
            	if(urlPath.indexOf('cloudlecture')!==-1){code='activity'}
            	else if(urlPath.indexOf('smartgroup')!==-1){code='expert'}
            	else if(urlPath.indexOf('cloudProject')!==-1){code='enterprise'}
            	else if(urlPath.indexOf('cloudprocurement')!==-1){code='server'};
                return function(){
                	App.storage.set('searchActiveCode',code);
                    App.navigate("example/index/search", true);
                    return false;
                }
            }

			if(content == "@back"||"@back1"||"back2"){
				return function(){
					App.back();
				};
			}
		},

		triggerButtonLeftClick:function(){
			this.trigger("button:left:click");
		},
		
		triggerButtonRightClick:function(){
			this.trigger("button:right:click");
		},
		
		execLeftButtonHandler:function(){
			var handle = this.leftButtonHandler;
			if(!handle)
				handle = this.defaultHandler(this.leftButtonContent);
			if(handle)
				handle();
		},

		execRightButtonHandler:function(){
			var handle = this.rightButtonHandler;
			if(!handle)
				handle = this.defaultHandler(this.rightButtonContent);
			if(handle)
				handle();
		},

		/**
		 * @desc 重新设置自己的内容
		 * @memberof Nuui.Header
		 * @func reset
		 * @instance
		 * @param {object} options - 参数
		 * @param {string} options.title - 标题文字<br/>
		 * @param {string} options.leftButtonContent - 左键的内容,有些特定内容会被添加事件,如@back<br/>
		 * @param {func} options.leftButtonHandler - 左键点击事件,优先级比leftButtonContent内容高<br/>
		 * @param {string} options.rightButtonContent - 右键的内容,有些特定内容会被添加事件,如@exit<br/>
		 * @param {func} options.rightButtonHandler - 右键点击事件,优先级比rightButtonContent内容高<br/>
		 */
		reset:function(options){

			this.title = options.title;

			this.leftButtonHandler = options.leftButtonHandler;
			this.leftButtonContent = options.leftButtonContent;
			
			this.rightButtonContent = options.rightButtonContent;
			this.rightButtonHandler = options.rightButtonHandler;
			
			if(Nuui.os == "web"){
				this.$title.html(options.title || "");
				this.$leftButton.html(this.contents[options.leftButtonContent] || options.leftButtonContent || "");
				this.$rightButton.html(this.contents[options.rightButtonContent] || options.rightButtonContent || "");
			}else{
				Nuui.client.resetHeader({
					title:this.title,
					leftButtonContent:this.leftButtonContent || "",
					rightButtonContent:this.rightButtonContent || ""
				});
			}

		}
	});
});
